#include "logMain.hpp"

using namespace eprosima::fastdds::dds;

logProtocol::logProtocol()
    : participant_(nullptr)
    , publisher_(nullptr)
    , topic_(nullptr)
    , writer_(nullptr)
    , type_(new text_messagePubSubType())
{
}

logProtocol::~logProtocol()
{
    if (writer_ != nullptr)
    {
        publisher_->delete_datawriter(writer_);
    }
    if (publisher_ != nullptr)
    {
        participant_->delete_publisher(publisher_);
    }
    if (topic_ != nullptr)
    {
        participant_->delete_topic(topic_);
    }
    DomainParticipantFactory::get_instance()->delete_participant(participant_);
}

logProtocol& logProtocol::Instance()
{
    static logProtocol _agent;
	return _agent;
}

bool logProtocol::init(int part, const char * pub_topicName)
{
    /* Initialize data_ here */
    if(pub_topicName == nullptr)
    {
        return false;
    }
    //CREATE THE PARTICIPANT
    DomainParticipantQos pqos;
    pqos.name("Participant_logPub");
    participant_ = DomainParticipantFactory::get_instance()->create_participant(part, pqos);
    if (participant_ == nullptr)
    {
        return false;
    }

    //REGISTER THE TYPE
    type_.register_type(participant_);

    //CREATE THE PUBLISHER
    publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT, nullptr);
    if (publisher_ == nullptr)
    {
        return false;
    }

    //CREATE THE TOPIC
    topic_ = participant_->create_topic(
        pub_topicName,
        type_.get_type_name(),
        TOPIC_QOS_DEFAULT);
    if (topic_ == nullptr)
    {
        return false;
    }

    // CREATE THE WRITER
    DataWriterQos wqos = DATAWRITER_QOS_DEFAULT;
    wqos.reliability().kind = RELIABLE_RELIABILITY_QOS;
    wqos.publish_mode().kind = ASYNCHRONOUS_PUBLISH_MODE;
    wqos.endpoint().history_memory_policy = eprosima::fastrtps::rtps::MemoryManagementPolicy::PREALLOCATED_WITH_REALLOC_MEMORY_MODE;
    writer_ = publisher_->create_datawriter(topic_, wqos, &listener_);
    if (writer_ == nullptr)
    {
        return false;
    }
    qlog_i(  "%s,publisher init !",pub_topicName);
    qlog_i("Log DDS init.");
    return true;
}

void logProtocol::PubListener::on_publication_matched(
        eprosima::fastdds::dds::DataWriter*,
        const eprosima::fastdds::dds::PublicationMatchedStatus& info)
{
    if (info.current_count_change == 1)
    {
        matched = info.total_count;
        std::cout << "Log DataWriter matched."<< std::endl;
    }
    else if (info.current_count_change == -1)
    {
        matched = info.total_count;
        std::cout << "Log DataWriter unmatched."<< std::endl;
    }
    else
    {
        qlog_w("%d is not a valid value for PublicationMatchedStatus current count change.",info.current_count_change);
    }
}

bool logProtocol::publish(std::string& str)
{
    if (listener_.matched > 0) {
        text_message st;
        st.from(CURRENT_DEVICE_TYPE);
        st.to(DEST_DEVICE_TYPE);
        st.msg(str);
        return writer_->write(&st);
    }
    return false;
}